package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"go_19/core"
	"go_19/global"
	"go_19/initialize"
	"go_19/service/pulsar/pulsar_topic_reader"
	"time"
)

func main() {
	core.EmptyCli()
	readerParam := getReaderParams()
	param, _ := json.Marshal(readerParam)
	fmt.Println(string(param))
	pulsar_topic_reader.TopicReader(readerParam)
}

// 获取cli参数
func getReaderParams() initialize.TopicReader {
	fmt.Println("Topic Reader")
	var param initialize.TopicReader

	broker := flag.String("broker", "", "required broker")
	topic := flag.String("topic", "", "required topic")
	startMessageID := flag.String("StartMessageID", "1", "StartMessageID 1:earliest 2:latest messageId")
	mapMessageIds := flag.String("mapMessageIds", "", "mapMessageIds 匹配消息IDs")
	flag.Parse()

	fmt.Println(broker)
	if *broker == "" {
		panic("please input broker!")
	}

	if *topic == "" {
		panic("please input topic!")
	}

	param.Topic = *topic
	param.SubscriptionRolePrefix = "reader"
	param.SubscriptionName = "reader_tool"
	param.ReaderName = "reader_tool"
	param.StartMessageID = *startMessageID
	param.MapMessageIds = *mapMessageIds

	param.ClientOption.URL = *broker
	param.ClientOption.ConnectionTimeout = time.Second * time.Duration(5)
	param.ClientOption.OperationTimeout = time.Second * time.Duration(30)
	param.ClientOption.MaxConnectionsPerBroker = 1
	param.ClientOption.LogPath = global.GVA_CONFIG.Log.LogDir
	param.ClientOption.LogName = "reader_topic_" + *topic
	return param
}
